<template>
    <div>
        <el-upload v-model:file-list="fileListModel" :auto-upload="false" list-type="picture-card"
            :on-change="fileChange" ref="uploadObj" accept="image/png, image/jpeg, image/jpg, image/tiff, image/webp">
            <el-icon>
                <Plus />
            </el-icon>
            <template #file="{ file }">
                <div style="display: flex;">
                    <el-image :src="file.url" fit="cover" v-loading="file.status === 'uploading'" />
                    <label v-if="file.status === 'success'" class="el-upload-list__item-status-label">
                        <i class="el-icon el-icon--upload-success el-icon--check">
                            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1024 1024">
                                <path fill="currentColor"
                                    d="M406.656 706.944 195.84 496.256a32 32 0 1 0-45.248 45.248l256 256 512-512a32 32 0 0 0-45.248-45.248L406.592 706.944z">
                                </path>
                            </svg>
                        </i>
                    </label>
                    <label v-else-if="file.status === 'fail'" class="el-upload-list__item-fail-label">
                        <i class="el-icon el-icon--fail">
                            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1024 1024">
                                <path fill="currentColor"
                                    d="M764.288 214.592 512 466.88 259.712 214.592a31.936 31.936 0 0 0-45.12 45.12L466.752 512 214.528 764.224a31.936 31.936 0 1 0 45.12 45.184L512 557.184l252.288 252.288a31.936 31.936 0 0 0 45.12-45.12L557.12 512.064l252.288-252.352a31.936 31.936 0 1 0-45.12-45.184z">
                                </path>
                            </svg>
                        </i>
                    </label>
                    <span class="el-upload-list__item-actions">
                        <span class="el-upload-list__item-preview" @click="handlePictureCardPreview(file)">
                            <el-icon><zoom-in /></el-icon>
                        </span>
                        <span v-if="file.status === 'success'" class="el-upload-list__item-download"
                            @click="handleDownload(file)">
                            <el-icon>
                                <Download />
                            </el-icon>
                        </span>
                        <span
                            v-if="isSingleUpload && (file.status === 'ready' || file.status === 'fail') && formType === 'add'"
                            class="el-upload-list__item-download" @click="handleUpload(file)">
                            <el-icon>
                                <Upload />
                            </el-icon>
                        </span>
                        <span v-if="formType === 'edit' || formType === 'add'" class="el-upload-list__item-delete"
                            @click="handleRemove(file)">
                            <el-icon>
                                <Delete />
                            </el-icon>
                        </span>
                    </span>
                </div>
            </template>
        </el-upload>
        <el-image-viewer v-if="showImgName" :initial-index="imageIndex" :infinite="true" @close="closeViewer"
            :zoom-rate="1.2" :max-scale="7" :min-scale="0.2" :url-list="fileListModel.map((item) => item.url)" />
    </div>
</template>

<script lang='ts' setup name=''>
import { ref } from 'vue';
import { ElMessage, UploadFiles } from 'element-plus';
import { Delete, Download, Plus, ZoomIn, Upload } from '@element-plus/icons-vue';
import DownloadUtil from '/@/utils/download';
import { getTableGridObjById } from '../../../hooks/dealData';

const props = defineProps({
    formType: String,
    isSingleUpload: {
        type: Boolean,
        default: () => false
    },
    tableGridId: {
        type: String,
        required: true
    }
})
const uploadObj = ref();
const showImgName = ref('');
const imageIndex = ref(0);
const tableGridObj = getTableGridObjById(props.tableGridId);
const fileListModel = defineModel({ required: true, default: [] as any[] });

const handleRemove = async (uploadFile: UploadFile) => {
    if (uploadFile.status === 'success') {
        // 后续网络删除接口在此实现
        await tableGridObj.deleteFile(uploadFile.id).then((res: any) => {
            if (res.status === 'success') {
                ElMessage({
                    message: '删除成功！',
                    type: 'success',
                })
            } else {
                ElMessage.error('删除失败，请联系管理员！')
                return
            }
        });
    }
    fileListModel.value = fileListModel.value.filter((item: any) => {
        return item.name !== uploadFile.name;
    })
}

const handlePictureCardPreview = (uploadFile: UploadFile) => {
    fileListModel.value.forEach((item: any, index: number) => {
        if (item.name === uploadFile.name) {
            imageIndex.value = index;
        }
    })
    showImgName.value = uploadFile.name;
}

const closeViewer = () => {
    showImgName.value = '';
}

const handleDownload = (uploadFile: UploadFile) => {
    DownloadUtil.image(uploadFile.url as string, uploadFile.name);
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars, no-unused-vars
const handleUpload = (uploadFile: UploadFile) => {
    uploadFile.status = 'uploading';
    tableGridObj.uploadFile(uploadFile).then(() => {
        uploadFile.status = 'success';
        ElMessage({
            message: '上传成功！！！',
            type: 'success',
        });
    }).catch(() => {
        uploadFile.status = 'fail';
        ElMessage.error('上传失败，请重新上传！！！');
    })
}

function fileChange(uploadFile: UploadFile, uploadFiles: UploadFiles) {
    fileListModel.value = uploadFiles;
}
</script>

<style lang="scss" scoped>
:deep(.el-upload-list__item-fail-label) {
    background: var(--el-color-danger-dark-2);
    height: 24px;
    right: -15px;
    text-align: center;
    top: -6px;
    transform: rotate(45deg);
    width: 40px;
    align-items: center;
    justify-content: center;
    line-height: inherit;
    position: absolute;
    display: flex;

    .el-icon--fail {
        color: #fff;
        top: 20%;
        transform: rotate(-45deg);
    }
}
</style>
